home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / stdio / fwrite.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  1KB  |  69 lines

  1.  
  2. /*
  3.  *  fwrite.c
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define buf ((const char *)vbuf)
  15.  
  16. size_t
  17. fwrite(vbuf, elmsize, elms, fi)
  18. const void *vbuf;
  19. size_t elmsize;
  20. size_t elms;
  21. FILE *fi;
  22. {
  23.     int n = EOF;
  24.     int bytes;
  25.  
  26.     if (elmsize == 1)
  27.     bytes = elms;
  28.     else if (elms == 1)
  29.     bytes = elmsize;
  30.     else
  31.     bytes = elms * elmsize;
  32.  
  33.     if (fi == NULL)
  34.     return(-1);
  35.     if (fi->sd_Flags & __SIF_WRITE) {
  36.     if (bytes > fi->sd_WLeft)
  37.         fflush(fi);             /*  also puts us into 'write' mode */
  38.  
  39.     if (bytes <= fi->sd_WLeft) {
  40.         movmem(buf, fi->sd_WPtr, bytes);
  41.         fi->sd_WLeft -= bytes;
  42.         fi->sd_WPtr += bytes;
  43.         n = bytes;
  44.  
  45.         if ((fi->sd_Flags & __SIF_IOLBF) && bytes && buf[bytes-1] == '\n')
  46.         fflush(fi);
  47.     } else {
  48.         long w = bytes;
  49.  
  50.         while (w > 0) {
  51.         n = write(fi->sd_Fd, buf, w);
  52.         if (n <= 0)
  53.             break;
  54.         fi->sd_Offset += n;
  55.         vbuf = buf + n;
  56.         w -= n;
  57.         }
  58.     }
  59.     }
  60.     if (n < 0)
  61.     fi->sd_Error = EOF;
  62.     if (fi->sd_Error)
  63.     return(fi->sd_Error);
  64.     if (n == bytes)
  65.     return(elms);
  66.     return(n / elmsize);
  67. }
  68.  
  69.